package LeetCode.leetcode;

/**
 * 第三大的数
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/28
 * @time: 15:52
 */
public class Test414第三大的数 {
    public static void main(String[] args) {
        int[] nums = {2, 3, 2, 1, 1};
        System.out.println(thirdMax(nums));
    }
    public static int thirdMax(int[] nums) {
        Integer max1 = null;
        Integer max2 = null;
        Integer max3 = null;
        for (Integer n : nums) {
            if (n.equals(max1) || n.equals(max2) || n.equals(max3)) {
                continue;
            }
            if (max1 == null || n > max1) {
                max3 = max2;
                max2 = max1;
                max1 = n;
            } else if (max2 == null || n > max2) {
                max3 = max2;
                max2 = n;
            } else if (max3 == null || n > max3) {
                max3 = n;
            }
        }
        return max3 == null ? max1 : max3;

    }
}
